<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false" width="700px"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" inline @keyup.enter.native="dataFormSubmit()" label-width="140px">
      <el-form-item label="车队名称" prop="fleetName">
        <el-input v-model="dataForm.fleetName" placeholder="车队名称" style="width: 480px;"></el-input>
      </el-form-item>
      <el-form-item label="收运单位" prop="ctUnitId">
        <el-select v-model="dataForm.ctUnitId" placeholder="请选择收运单位" style="width: 480px;" @change="unitChange">
          <el-option v-for="item in unitList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="队长" prop="principalId">
        <el-select filterable clearable v-model="dataForm.principalId" placeholder="请选择负责人" style="width: 480px;">
          <template v-for="user in userList">
            <el-option v-if="user.ctUnitId === dataForm.ctUnitId" :key="user.userId" :label="user.username" :value="user.userId">
              <span>{{ user.username }}</span><span style="float: right;">{{ user.mobile }}</span>
            </el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item label="管理区域" prop="manageArea">
        <el-input v-model="dataForm.manageArea" placeholder="管理区域" style="width: 480px;"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="dataForm.remark" placeholder="备注" maxlength="180" show-word-limit style="width: 480px;"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    unitList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      visible: false,
      userList: [],
      dataForm: {
        id: 0,
        ctUnitId: "",
        fleetName: "",
        principalId: "",
        manageArea: "",
        remark: ""
      },
      dataRule: {
        ctUnitId: [
          { required: true, message: "收运单位不能为空", trigger: "blur" }
        ],
        fleetName: [
          { required: true, message: "车队名称不能为空", trigger: "blur" }
        ],
        principalId: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
        // manageArea: [
        //   { required: true, message: '管理区域不能为空', trigger: 'blur' }
        // ],
        // remark: [{ required: true, message: "备注不能为空", trigger: "blur" }]
      }
    };
  },
  mounted () {
    this.getUserListByRoleName('车队队长')
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/sign/signfleet/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.ctUnitId = data.signFleet.ctUnitId
              this.dataForm.fleetName = data.signFleet.fleetName
              this.dataForm.principalId = data.signFleet.principalId
              this.dataForm.manageArea = data.signFleet.manageArea
              this.dataForm.remark = data.signFleet.remark
            }
          });
        }
      });
    },
    getUserListByRoleName (roleName) {
      this.$http({
        url: this.$http.adornUrl(`/sys/user/rolename`),
        method: 'post',
        data: this.$http.adornData({ roleName: roleName })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.userList = data.list || []
        } else {
          this.userList = []
        }
      })
    },
    unitChange () {
      this.dataForm.principalId = ''
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sign/signfleet/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              ctUnitId: this.dataForm.ctUnitId,
              fleetName: this.dataForm.fleetName,
              principalId: this.dataForm.principalId,
              manageArea: this.dataForm.manageArea,
              remark: this.dataForm.remark
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
